package com.flexrrb.containers.cardDeckClasses
{
	import com.flexrrb.events.CardEvent;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.styles.CSSStyleDeclaration;
	import mx.styles.StyleManager;

	public class Card extends Canvas
	{
		public static var classConstructed:Boolean = constructClass();
		
		private var _title:String;
		private var _state:int;
		private var _originalIndex:int;
		
		private static function constructClass():Boolean
		{
			var selector:CSSStyleDeclaration = StyleManager.getStyleDeclaration("Card");
			if(!selector)
			{
				selector = new CSSStyleDeclaration();
			}
			
			selector.defaultFactory = function():void
			{
				this.backgroundColor = 0xFFFFFF;
				this.horizontalScrollPolicy = "off";
				this.verticalScrollPolicy = "off";
			}
			
			StyleManager.setStyleDeclaration("Card", selector, false);
			
			return true;
		}
		
		public function Card()
		{
			super();
			this.mouseChildren = false;
			this.addEventListener(MouseEvent.CLICK, onMouseClick, false, 0, true);
		}
		
		public function get title():String
		{
			return _title;
		}
		
		public function set title(newTitle:String):void
		{
			_title = newTitle;
		}
		
		public function set state(newState:int):void
		{
			_state = newState;
			switch(_state)
			{
				case CardState.NORMAL: case CardState.DESELECTING:
					this.mouseChildren = false;
					this.addEventListener(MouseEvent.CLICK, onMouseClick, false, 0, true);
				break;
				
				case CardState.SELECTED:
					this.mouseChildren = true;
					this.removeEventListener(MouseEvent.CLICK, onMouseClick);
				break;
			}
		}
		
		public function get state():int
		{
			return _state;
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			dispatchEvent(new CardEvent(CardEvent.SELECTED));
		}
		
		public function get originalIndex():int
		{
			return _originalIndex;
		}
		
		public function set originalIndex(value:int):void
		{
			_originalIndex = value;
		}
	}
}